package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.elepower.dao.UserDao;
import org.elepower.pojo.PsRole;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/psUser")
public class PsUserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("/index")
    public String index(ModelMap modelMap) throws Exception {
        modelMap.put("userList",userDao.getAll());
        return "system/user/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit, PsUser condition,String roleIds) throws Exception{

        Map<String,Object> map = new HashMap<>();

        Page<PsUser> pager = PageHelper.startPage(page,limit);

        //将查询的角色编号进行封装:字符串===集合
        if(roleIds != null && !roleIds.trim().equals("")){
            String[] array = roleIds.split(",");
            //封装到集合
            List<PsRole> roles = new ArrayList<>();
            for (int i = 0; i < array.length;i++){
                PsRole sysRole = new PsRole();
                sysRole.setId(Integer.parseInt(array[i]));
                roles.add(sysRole);
            }
            condition.setRoles(roles);
        }

        List<PsUser> list = userDao.getlist(condition);
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);
        map.put("count",pager.getTotal());
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@Param("ids") Integer[] ids) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        //删除用户角色表
        for(int i = 0;i < ids.length;i++){
            userDao.deleteUserRole(ids[i]);
        }
        userDao.delete(ids);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/userForm")
    public String userForm() throws Exception{
        return "system/user/form";
    }


    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addUser(PsUser psUser,String roleStr) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //获取角色编号
        String[] array = roleStr.split(",");
        Integer[] ids = new Integer[array.length];
        for (int i = 0;i < ids.length;i++){
            ids[i] = Integer.parseInt(array[i]);
        }
        //对密码加密
        Md5Hash md5Hash = new Md5Hash(psUser.getPassword(),psUser.getSalt());
        psUser.setPassword(md5Hash.toString());
        if(psUser.getUserState() == null){
            psUser.setUserState(0);
        }
        userDao.add(psUser);
        System.out.println(psUser.getId()+"\t"+psUser.getUsername());
        userDao.addUserRole(psUser.getId(),ids);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/getByUserName")
    @ResponseBody
    public PsUser getByUserName(String userName,Integer id) throws Exception{
        return userDao.getUserName(userName,id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsUser psUser,String roleStr) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //删除用户角色
        userDao.deleteUserRole(psUser.getId());
        //添加用户角色
        //获取角色编号
        String[] array = roleStr.split(",");
        Integer[] ids = new Integer[array.length];
        for (int i = 0;i < ids.length;i++){
            ids[i] = Integer.parseInt(array[i]);
        }
        userDao.addUserRole(psUser.getId(),ids);

        //修改用户信息

        //获取未修改的用户信息
        PsUser psUser1 = userDao.getUser(psUser.getId());
        //不修改密码和盐值
        if(!psUser.getPassword().equals(psUser1.getPassword())) {
            //进行密码加密
            Md5Hash md5Hash = new Md5Hash(psUser.getPassword(), psUser.getSalt());
            psUser.setPassword(md5Hash.toString());
        }

        if(psUser.getUserState() == null){
            psUser.setUserState(0);
        }

        psUser.setLastUpdateTime(new Date());
        userDao.update(psUser);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/get")
    @ResponseBody
    public PsUser get(Integer id) throws Exception{
        return userDao.getUser(id);
    }






}
